package com.neteast.androidclient.contact.widget;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.domain.ContactData;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactPanel extends ViewGroup {
	private String TAG = "ContactPanel";
	private Context mContext;
	// x,y轴[x控制列,y控制行]
	public final static int x = 4;
	public final static int y = 3;
	public final static int mMarginTop = 27;// 单屏整块与顶部的距离
	public final static int mMarginLeft = 45;// 单屏整块与左边距离
	public final static int mPaddingTop = 12; // 每个子块之间的上下距离
	public final static int mPaddingLeft = 11; // 每个子块之间的左右距离
	public static int mControlWidth;
	// 屏数
	private int mScreen;
	private OnContactItemClickListener onItemClickListener;
	private OnDeleteItemListener onDeleteItemListener;
	ArrayList<ContactData> phones;
	ArrayList<ContactData> emails;
	private ArrayList<Contact> data;
	
	private static ImageView avatarImg;
	private static TextView nameText;
	private static TextView companyText;
	private static TextView positionText;
	private static TextView phoneText;
	private static TextView emailText;
	private static ImageView deleteIcon;
	private static ImageView cloudIcon; 

	public ContactPanel(Context context, final ArrayList<Contact> list) {
		super(context);
		this.mContext = context;
		data = list;
		int showNum = 24;
		// 总屏数
		this.mScreen = (int) Math.ceil(((double) showNum) / (x * y));
		// 初始化childview
		for (int i = 0; i < showNum; i++) {
			Contact info = data.get(i);
			final int position = i;
			View view = getView(info, position);
			addView(view);
		}
	}

	// 测量
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		mControlWidth = widthSize;
		// 设置每个子View的宽高, offX和offY分别表示在横竖方向上需要空出的总距离
		int offX = mMarginLeft * 2 + mPaddingLeft * (x - 1);
		int offY = mMarginTop * 2 + mPaddingTop * (y - 1);
		final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
				(widthSize - offX) / x, MeasureSpec.EXACTLY);
		final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
				(heightSize - offY) / y, MeasureSpec.EXACTLY);

		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			getChildAt(i)
					.measure(childWidthMeasureSpec, childHeightMeasureSpec);
		}
		// 设置总屏宽高
		setMeasuredDimension(widthSize * mScreen, heightSize);
	}

	@Override
	protected void onLayout(boolean change, int l, int t, int r, int b) {
		Log.d(TAG, "-------------------------------change--"+change);
//		if (change) {
			final int count = getChildCount();// view数量
			int childLeft = l + mMarginLeft;
			int childTop = t + mMarginTop;
			View view = null;
			int index = 0;
			// 屏,行,列
			for (int i = 0; i < mScreen; i++) {
				for (int j = 0; j < y; j++) {
					for (int k = 0; k < x; k++) {
						if (index >= count) {
							break;
						}
						view = getChildAt(index);
						view.layout(childLeft, childTop,
								childLeft + view.getMeasuredWidth(), childTop
										+ view.getMeasuredHeight());
						childLeft += (view.getMeasuredWidth() + mPaddingLeft);// 设置每块的左右边距11
						index++;
					}
					childLeft = mControlWidth * i + mMarginLeft;
					childTop = (view.getMeasuredHeight() + mPaddingTop)
							* (j + 1) + mMarginTop;// 设置每块的上下边距是12
				}
				childLeft = mControlWidth * (i + 1) + mMarginLeft;
				childTop = t + mMarginTop;
			}
//		}
	}

	// 每个子项列表监听
	public static interface OnContactItemClickListener {
		public void onContactItemClick(View view, int position, long id);
	}

	public void setOnContactItemClickListener(
			OnContactItemClickListener onContactItemClickListener) {
		onItemClickListener = onContactItemClickListener;
	}

	// 每个子项的删除监听
	public static interface OnDeleteItemListener {
		public void onDeleteItem(View view, int position, long id);
	}

	public void setOnDeleteItemListener(
			OnDeleteItemListener onDeleteItemClickListener) {
		onDeleteItemListener = onDeleteItemClickListener;
	}

	private View getView(Contact info, final int position){
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.contact_card_item, null);
		avatarImg = (ImageView) view.findViewById(R.id.imageView);
		nameText = (TextView) view.findViewById(R.id.name);
		companyText = (TextView) view.findViewById(R.id.company);
		positionText = (TextView) view.findViewById(R.id.position);
		phoneText = (TextView) view.findViewById(R.id.phone);
		emailText = (TextView) view.findViewById(R.id.email);
		deleteIcon = (ImageView) view.findViewById(R.id.iconDelete);
		cloudIcon = (ImageView) view.findViewById(R.id.isCloud);
		
		avatarImg.setImageResource(R.drawable.default_photo);
		nameText.setText(info.displayName);
		companyText.setText(info.organization);
		positionText.setText(info.job);
		phones = info.phones;
		emails = info.emails;
		if (phones != null && phones.size() != 0) {
			phoneText.setText(phones
					.get(0).content);
		} else {
			phoneText.setText("未填写");
		}
		if (emails != null && emails.size() != 0) {
			emailText.setText(emails
					.get(0).content);
		} else {
			emailText.setText("未填写");
		}

		if (info.isReadOnly) {
			cloudIcon.setVisibility(View.VISIBLE);
		}
		view.setFocusable(true);
		view.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (onItemClickListener != null) {
					onItemClickListener.onContactItemClick(v, position,
							position);
				}
			}
		});
		deleteIcon.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (onDeleteItemListener != null) {
					onDeleteItemListener
							.onDeleteItem(v, position, position);
				}
			}
		});
		return view;
	}
	
	
	public void addContact(Context context, Contact contact) {

		data.add(contact);
		int everyScreenCount = x * y;
		int lastScreenCount = getChildCount() % everyScreenCount;//得到总的屏数
		if (lastScreenCount < everyScreenCount) {//若项目数小于每屏规定12个，则在当前屏添加
			View view = LayoutInflater.from(mContext).inflate(
					R.layout.contact_card_item, null);
			view.setLayoutParams(new LayoutParams(200,300));
			((ImageView) view.findViewById(R.id.imageView))
					.setImageResource(R.drawable.default_photo);
			addView(view);
			view.layout(0, 0, 0, 0);
			invalidate();
		} else {
			// create a new screen
		}
		bringToFront();
	}
	
	/**
	 * 删除某项联系人的操作
	 * @param position 删除的Item定位
	 * @param panel 删除元素所属的view集
	 * @param list 列表数据
	 * */
	public static void deleteItem(int position, ContactPanel panel, ArrayList<Contact> list){
		View view;
		for(int i = 0; i< list.size()-1;i++){
			if(i >= position ){
				view = panel.getChildAt(i);
				avatarImg = (ImageView) view.findViewById(R.id.imageView);
				nameText = (TextView) view.findViewById(R.id.name);
				companyText = (TextView) view.findViewById(R.id.company);
				positionText = (TextView) view.findViewById(R.id.position);
				phoneText = (TextView) view.findViewById(R.id.phone);
				emailText = (TextView) view.findViewById(R.id.email);
				deleteIcon = (ImageView) view.findViewById(R.id.iconDelete);
				cloudIcon = (ImageView) view.findViewById(R.id.isCloud);
				
				nameText.setText(list.get(i+1).displayName);
				companyText.setText(list.get(i+1).organization);
				positionText.setText(list.get(i+1).job);
				phoneText.setText(list.get(i+1).displayName);
				emailText.setText(list.get(i+1).displayName);
			}
		}
		list.remove(position);
		panel.removeViewAt(panel.getChildCount()-1);
	}
	
	/**
	 * 删除某项联系人的操作
	 * @param position 删除的Item定位
	 * @param panel 删除元素所属的view集
	 * @param list 列表数据
	 * */
	public static void refreshView(ContactPanel panel, ArrayList<Contact> list, int page){
		View view;
		int moveValue = page * 12; 
		for(int i = 0; i< panel.getChildCount();i++){
				view = panel.getChildAt(i);
				avatarImg = (ImageView) view.findViewById(R.id.imageView);
				nameText = (TextView) view.findViewById(R.id.name);
				companyText = (TextView) view.findViewById(R.id.company);
				positionText = (TextView) view.findViewById(R.id.position);
				phoneText = (TextView) view.findViewById(R.id.phone);
				emailText = (TextView) view.findViewById(R.id.email);
				deleteIcon = (ImageView) view.findViewById(R.id.iconDelete);
				cloudIcon = (ImageView) view.findViewById(R.id.isCloud);
				
				nameText.setText(list.get(i+moveValue).displayName);
				companyText.setText(list.get(i+moveValue).organization);
				positionText.setText(list.get(i+moveValue).job);
				phoneText.setText(list.get(i+moveValue).displayName);
				emailText.setText(list.get(i+moveValue).displayName);
		}
	}
}
